﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Reflection;

namespace RunUOGenerator
{
    class ConfigReader : XMLReader
    {
        public ConfigReader()
        {
        }

        public ArrayList BuildModules()
        {
            ArrayList list = new ArrayList();
            try
            {
                this.Load("Config.xml");
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            foreach (XmlNode node in this.ReadNode("modules"))
            {
                // Make sure we are in modules.
                if (node.HasChildNodes && node.Name.ToLower() == "modules")
                {
                    foreach (XmlNode child in node.ChildNodes)
                    {
                        // Make sure this is a module node.
                        if (child.Name.ToLower() == "module")
                        {
                            Module module = new Module();
                            // Set the path to the module and get the directory the current module says its in.
                            string modulePath = Program.ExecutionLocation + "\\" + Program.ModuleLocation + "\\";
                            string moduleDirectory = child.Attributes["Directory"].Value;

                            foreach (XmlAttribute attribute in child.Attributes)
                            {
                                switch (attribute.Name.ToLower())
                                {
                                    case "name":
                                        module.Name = attribute.Value;
                                        break;
                                    case "title":
                                        module.Title = attribute.Value;
                                        break;
                                    case "description":
                                        module.Description = attribute.Value;
                                        break;
                                    case "bitmap":
                                        try
                                        {
                                            // Lets make sure this isn't null.
                                            if (attribute.Value != "")
                                            {
                                                module.StartupImage = new Bitmap(modulePath + moduleDirectory + "\\" + attribute.Value);
                                            }
                                        }catch (Exception){Debug.WriteLine("There was an error while trying to read the bitmap.");} 
                                        break;
                                    case "attributesfile":
                                        try
                                        {
                                            // Lets make sure this isn't null.
                                            if (attribute.Value != "")
                                            {
                                                XmlDocument doc = new XmlDocument();
                                                doc.Load(modulePath + moduleDirectory + "\\" + attribute.Value);
                                                module.Attributes = doc; 
                                            }
                                        }
                                        catch (Exception) { Debug.WriteLine("There was an error while trying to read the attributes file."); }
                                        break;
                                    case "layoutfile":
                                        try
                                        {
                                            // Lets make sure this isn't null.
                                            if (attribute.Value != "")
                                            {
                                                XmlDocument doc = new XmlDocument();
                                                doc.Load(modulePath + moduleDirectory + "\\" + attribute.Value);
                                                module.Layout = doc;

                                                Debug.WriteLine(modulePath + moduleDirectory + "\\" + attribute.Value);
                                            }
                                        }
                                        catch (Exception) { Debug.WriteLine("There was an error while trying to read the layout file."); } 
                                        break;
                                }
                            }

                            list.Add(module);
                        }
                    }
                }
            } 

            return list;
        }
    }
}
